home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 2 / Gold Medal Software Volume 2 (Gold Medal) (1994).iso / os2 / cenvi2.arj / NEKOSAVR.CMD < prev    next >
OS/2 REXX Batch file  |  1993-12-21  |  5KB  |  154 lines

  1. EXTPROC CENVI
  2. /********************************
  3.  *** NekoSavr.cmd - blah blah ***
  4.  ********************************/
  5.  
  6.  
  7. main(argc,argv)
  8. {
  9.    // test that the input looks valid
  10.    if ( argc != 2
  11.      || !isdigit(argv[1][0])
  12.      || (QuietInterval = atoi(argv[1])) < 1 )
  13.       ShowInstructions();
  14.    else {
  15.       // kill the resident Neko screen saver it it;s already running
  16.       system("kill \"%s\"",WindowTitle);
  17.       suspend(2000);
  18.       // install the resident version of this program
  19.       system("start \"%s\" /N /B /WIN /MIN CEnvi #include '%s,,,/*RESIDENT*/' Forever(%d)",
  20.              WindowTitle,argv[0],60 * QuietInterval);
  21.  
  22.       // hide the resident version of this program
  23.       suspend(5000);
  24.       system("WinSet \"%s\" HIDE",WindowTitle);
  25.       printf("Cat & Mouse games will begin after %d minutes of keyboard and\n"
  26.              "mouse inactivity.\n",QuietInterval);
  27.    }
  28. }
  29.  
  30. ShowInstructions()
  31. {
  32.    printf("\a\n");
  33.    printf("NekoSavr.cmd - Cat & Mouse Screen Saver. Run NEKO.EXE if there is no\n");
  34.    printf("               keyboard keys or mouse keys pressed. End NEKO.EXE when\n");
  35.    printf("               keys pressed again.\n");
  36.    printf("\n");
  37.    printf("SYNTAX: NekoSavr Interval\n");
  38.    printf("\n");
  39.    printf("Where:   Interval - How many minutes of no keyboard or mouse-button activity\n");
  40.    printf("                    before the Cat & Mouse screen saver begins\n");
  41.    printf("\n");
  42.    printf("Example: NekoSavr 5      - Start neko after 5 minutes of inactivity\n");
  43.    printf("         NekoSavr 1      - Start neko after 1 minute of inactivity\n");
  44.    printf("\n");
  45. }
  46.  
  47.  
  48. /*RESIDENT*/
  49. // All code below this is part of the resident program
  50.  
  51. #include <KeyPush.lib>
  52.  
  53. WindowTitle = "Neko Screen Saver";
  54.  
  55. #define  START_WAIT_INTERVAL  4000  // set for how often to check the table for changes
  56.                                     // in milliseconds while waiting to start screen saver
  57. #define  END_WAIT_INTERVAL    750   // interval to wait while waiting to end screen saver
  58.  
  59. #define  KEYBOARD_TABLE_SIZE  256
  60.  
  61. Forever(QuietInterval)
  62.    // wait until QuietInterval seconds has elapsed with no apparent keyboard
  63.    // activity.
  64. {
  65.    while(TRUE) {
  66.       WaitForInactivity(QuietInterval); // wait for period of inactivity
  67.       StartProgram();                   // start program, and get handle
  68.       WaitForActivity();      // wait for a keyboard or mouse press
  69.       EndProgram();
  70.    }
  71. }
  72.  
  73. WaitForInactivity(QuietInterval) // check every once in a while, and return after a
  74. {                                // long period of inactivity
  75.    // check at every interval and set keyboard if it is not already set
  76.    PrevTable = GetGlobalKeyboardTable();
  77.    PrevPos = PointerPosition();
  78.    EndTime = time() + QuietInterval;
  79.    // stay in this block until period of inactivity has occurred
  80.    do {
  81.       suspend(START_WAIT_INTERVAL); // wait a while
  82.       CurrentTable = GetGlobalKeyboardTable();
  83.       CurrentPos = PointerPosition();
  84.       if ( memcmp(CurrentTable,PrevTable,KEYBOARD_TABLE_SIZE)
  85.         || CurrentPos != PrevPos ) {
  86.          // table has changed; reset EndTime and remember table
  87.          EndTime = time() + QuietInterval;
  88.          PrevTable = CurrentTable;
  89.          PrevPos = CurrentPos;
  90.       }
  91.    } while ( time() < EndTime );
  92. }
  93.  
  94. WaitForActivity() // check every once in a while, and return after a
  95. {                 // change in the keyboard or mouse
  96.    PreviousTable = GetGlobalKeyboardTable();
  97.    PrevPos = PointerPosition();
  98.    while( !memcmp(PreviousTable,GetGlobalKeyboardTable(),KEYBOARD_TABLE_SIZE)
  99.        && PrevPos == PointerPosition() ) {
  100.       printf("\nwaiting for change");
  101.       suspend(END_WAIT_INTERVAL);
  102.    }
  103. }
  104.  
  105.  
  106. StartProgram()
  107. {
  108.    // start NEKO running
  109.    system("start /B /PM NEKO.EXE");
  110.    suspend(3000);
  111.  
  112.    // pass keystrokes to NEKO to make desktop go away
  113.    KeyStroke(VK_ENTER);
  114.    suspend(5000);
  115.    KeyStroke(VK_ENTER);
  116.    suspend(1500);
  117. }
  118.  
  119.  
  120. EndProgram()
  121. {
  122.    KeyStroke(VK_ALT);
  123.    KeyStroke(VK_ALT,VK_F4);
  124. }
  125.  
  126.  
  127. ForceRandomChange = 0; // if can't read table then pretend that it has changed
  128.  
  129. GetGlobalKeyboardTable()
  130. {
  131.    #define HWND_DESKTOP                     1
  132.    #define ORD_WIN32SETKEYBOARDSTATETABLE   921
  133.    KeyTable[KEYBOARD_TABLE_SIZE-1] = '\0'; // pre-allocate a 256 byte buffer
  134.    if !DynamicLink("PMWIN",ORD_WIN32SETKEYBOARDSTATETABLE,BIT32,CDECL,
  135.                    HWND_DESKTOP,KeyTable,FALSE) {
  136.       // could not read global key table, and so pretend it has changed
  137.       KeyTable[0] = (ForceRandomChange ^= 1);
  138.    }
  139.    return(KeyTable);
  140. }
  141.  
  142. PointerPosition() // return current cursor position in screen coordinates
  143. {                 // structure elements returned are .col and .row
  144.    #define ORD_WIN32QUERYPOINTERPOS 823
  145.    BLObSize(pointer,4 * 2);
  146.    DynamicLink("PMWIN",ORD_WIN32QUERYPOINTERPOS,BIT32,CDECL,
  147.                HWND_DESKTOP,pointer);
  148.    position.col = BLObGet(pointer,0,SWORD16);
  149.    position.row = BLObGet(pointer,4,SWORD16);
  150.    return(position);
  151. }
  152.  
  153.  
  154.